home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / bin / screen-profiles-export < prev    next >
Encoding:
Text File  |  2009-04-08  |  5.8 KB  |  238 lines

  1. #!/bin/sh -e
  2. #
  3. #    screen-profile-export
  4. #    Copyright (C) 2008 Canonical Ltd.
  5. #
  6. #    Authors: Dustin Kirkland <kirkland@canonical.com>
  7. #
  8. #    This program is free software: you can redistribute it and/or modify
  9. #    it under the terms of the GNU General Public License as published by
  10. #    the Free Software Foundation, version 3 of the License.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20. PROG="screen-profiles"
  21. SHARE="/usr/share/$PROG"
  22. TMPDIR=`mktemp -d screen-profiles.XXXXXXXX` || error "Could not create a temporary directory"
  23. # Grab list of available colors
  24. colors=`find $SHARE/profiles/ -type f | sed "s/^.*\///" | sed "s/^.*-//" | sort -u | egrep -v "common|plain"`
  25.  
  26. # Make sure we clean up $TMPDIR if we exit for any reason
  27. trap "rm -rf "$TMPDIR" 2>/dev/null || true" EXIT HUP INT QUIT TERM
  28.  
  29. usage() {
  30.     echo
  31.     echo "Usage:"
  32.     echo " $0 [-c COLOR] -f TARGET.tar.gz"
  33.     echo
  34.     echo "TARGET.tar.gz is required"
  35.     echo
  36.     echo "COLOR is obtained interactively if unspecified; or one of:"
  37.     for c in $colors; do
  38.         echo " * $c"
  39.     done
  40.     echo
  41.     exit 1
  42. }
  43.  
  44. error() {
  45.     echo "ERROR: $1" 1>&2
  46.     exit 1
  47. }
  48.  
  49. choose() {
  50.     i=0
  51.     x=
  52.     selected=
  53.     for x in $@; do
  54.         i=$(expr $i + 1)
  55.         [ $i -lt 10 ] && i=" $i"
  56.         echo " $i. $x" 1>&2
  57.     done
  58.     echo
  59.     count=1
  60.     while /bin/true; do
  61.         if [ $count -gt 5 ]; then
  62.             echo `gettext "ERROR: Invalid selection"`
  63.             exit 1
  64.         fi
  65.         count=`expr $count + 1`
  66.         if ! test $selected -gt 0 2>/dev/null; then
  67.             read -p "`gettext 'Choose'` [1 -$i]: " -r selected
  68.         elif ! test $selected -le $i 2>/dev/null; then
  69.             read -p "`gettext 'Choose'` [1 -$i]: " -r selected
  70.         else
  71.             break
  72.         fi
  73.     done
  74.     i=0
  75.     for x in $@; do
  76.         i=$(expr $i + 1)
  77.         if [ "$i" = "$selected" ]; then
  78.             SELECTED="$x"
  79.             return 0
  80.         fi
  81.     done
  82.     exit 1
  83. }
  84.  
  85. status_config() {
  86.     # Generate the status configuration
  87.     # Disable the menu, since screen-profiles configurator is not available
  88.     # Enable user@host in its place
  89.     for i in $(ls /var/lib/screen-profiles/); do
  90.         case "$i" in
  91.             cpu-count|cpu-freq|hostname|load-average|logo|mem-available|mem-used|reboot-required|release|updates-available|whoami)
  92.                 echo "$i=1"
  93.             ;;
  94.             *)
  95.                 echo "$i=0"
  96.             ;;
  97.         esac
  98.     done
  99. }
  100.  
  101. hr() {
  102.     echo "###############################################################################"
  103. }
  104.  
  105. header() {
  106.     hr
  107.     echo "# This GNU Screen profile was generated by the screen-profile-export"
  108.     echo "# program, which is part of the screen-profiles package, and contains a"
  109.     echo "# subset of the functionality available from the full package."
  110.     echo "#"
  111.     echo "# For more information, source code, questions, and bugs, see:"
  112.     echo "#  * https://launchpad.net/screen-profiles"
  113.     echo "#"
  114.     echo "#    Copyright (C) 2008 Canonical Ltd."
  115.     echo "#"
  116.     echo "#    Author: Dustin Kirkland <kirkland@canonical.com>"
  117.     echo "#"
  118.     echo "#    This program is free software: you can redistribute it and/or modify"
  119.     echo "#    it under the terms of the GNU General Public License as published by"
  120.     echo "#    the Free Software Foundation, version 3 of the License."
  121.     echo "#"
  122.     echo "#    This program is distributed in the hope that it will be useful,"
  123.     echo "#    but WITHOUT ANY WARRANTY; without even the implied warranty of"
  124.     echo "#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
  125.     echo "#    GNU General Public License for more details."
  126.     echo "#"
  127.     echo "#    You should have received a copy of the GNU General Public License"
  128.     echo "#    along with this program.  If not, see <http://www.gnu.org/licenses/>."
  129.     hr
  130. }
  131.  
  132. sources() {
  133.     # insert the common profile, replace the status exe path
  134.     echo
  135.     cat $SHARE/profiles/common | sed "s:$PROG-status:\$HOME/.$PROG/$PROG-status:"
  136.     hr
  137.     echo
  138.     cat $SHARE/keybindings/common
  139.     hr
  140.     echo
  141.     hr
  142.     cat "$SOURCE"
  143.     hr
  144. }
  145.  
  146.  
  147. # Handle command line parameters
  148. color=
  149. file=
  150. while [ $# -gt 1 ]; do
  151.     case "$1" in
  152.         -c)
  153.             color="$2"
  154.             shift 2
  155.         ;;
  156.         -f)
  157.             file="$2"
  158.             shift 2
  159.         ;;
  160.         *)
  161.             usage
  162.         ;;
  163.     esac
  164. done
  165.  
  166. [ $# -eq 1 ] && file="$1"
  167.  
  168. [ -z "$file" ] && usage
  169. echo "$file" | grep -qs "\.tar\.gz$" || error "Target file must be a '.tar.gz' archive"
  170. if [ -e "$file" ]; then
  171.     echo `gettext "File exists"` " [$file]"
  172.     read -p "`gettext 'Remove file? [y/N] '`" -r remove
  173.     if [ "$remove" = "Y" -o "$remove" = "y" ]; then
  174.         rm -f "$file"
  175.     else
  176.         exit 1
  177.     fi
  178. fi
  179.  
  180. # Obtain selections
  181. count=1
  182. distro="ubuntu"
  183. while /bin/true; do
  184.     if [ $count -gt 5 ]; then
  185.         echo `gettext "ERROR: Invalid selection"`
  186.         exit 1
  187.     fi
  188.     SOURCE="$SHARE"/profiles/"$distro"-"$color"
  189.     if [ -f "$SOURCE" ]; then
  190.         break
  191.     else
  192.         # Try in the misc directory too
  193.         SOURCE="$SHARE"/profiles/misc/"$distro"-"$color"
  194.         if [ -f "$SOURCE" ]; then
  195.             break
  196.         fi
  197.     fi
  198.     echo
  199.     echo `gettext "Select a color: "`
  200.     choose $colors
  201.     color=$SELECTED
  202.     count=`expr $count + 1`
  203. done
  204.  
  205. # Create workspace
  206. PROFILE="$TMPDIR/.screenrc"
  207. STATUS="$TMPDIR/.$PROG/status"
  208. mkdir -p "$TMPDIR/.$PROG/bin"
  209.  
  210. # Copy status scripts
  211. cp -a /var/lib/$PROG/* "$TMPDIR/.$PROG/bin"
  212. cp -a /usr/bin/$PROG-status "$TMPDIR/.$PROG"
  213.  
  214. # Generate the monolithic profile
  215. header > "$PROFILE"
  216. sources >> "$PROFILE"
  217. status_config > "$STATUS"
  218.  
  219. # Some gardening
  220. # Drop additional "source" calls
  221. sed -i "s:^source .*::" "$PROFILE"
  222. # Use .screenrc instead
  223. sed -i "s:.$PROG/profile\":\.screenrc\":" "$PROFILE"
  224. # Drop the F9->Menu key
  225. sed -i "s:^bindkey -k k9 screen -t help:#bindkey -k k9 screen -t help:" "$PROFILE"
  226.  
  227. # tar up the results
  228. tar -zcf "$file" -C "$TMPDIR" . || error "Could not create archive"
  229.  
  230. echo
  231. echo "Success!"
  232. echo
  233. echo "  "`gettext "Profile"` ": [$distro-$color]"
  234. echo "  "`gettext "Archive"` ": [$file]"
  235. echo
  236. echo `gettext "Extract the archive in your home directory on the target system."`
  237. echo
  238.